<?php

    class Application_Model_Common extends Zend_Db_Table_Abstract
    {

        protected $_name = "admin";

        function init()
        {
            
        }

        public function getMaxId($tableName, $idFieldName)
        {
            $this->_name = $tableName;

            $maxId = $this->select()
                            ->from($this, array(new Zend_Db_Expr("max($idFieldName) as maxid")))
                            ->query()->fetch();
            return $maxId;
        }

        public function getSumOf($idFieldName, $tableName, $whereCond = 1)
        {
            $this->_name = $tableName;

            $totalSum = $this->select()
                            ->from($this, array(new Zend_Db_Expr("sum($idFieldName) as totalsum")))
                            ->where($whereCond)
                            ->query()->fetch();

            return $totalSum;
        }

        public function getTotalCount($idFieldName, $tableName, $whereCond = 1)
        {
            $this->_name = $tableName;

            $totalcount = $this->select()
                            ->from($this, array(new Zend_Db_Expr("count($idFieldName) as totalcount")))
                            ->where($whereCond)
                            ->query()->fetch();
            return $totalcount;
        }

        public function getAllRecordDetail($tableName, $cond = 1, $order = NULL, $limit = NULL, $pagination = NULL)
        {
            $this->_name = $tableName;
            $records = $this->select()->where($cond);

            //prd($records->__toString());

            if ($limit != NULL)
                $records = $records->limit($limit);

            if ($order != NULL)
                $records = $records->order($order);

            if ($pagination)
                return $records;
            $records = $records->query()->fetchAll();
            return $records;
        }

        public function getAllRecordFromLeftJoin($fromTableArr, $otherTableArr, $joinCond, $whereCond = NULL, $order = NULL, $limit = NULL)
        {

            if ($whereCond == NULL)
            {
                $records = $this->getAdapter()->select()
                        ->from($fromTableArr)
                        ->joinLeft($otherTableArr, $joinCond);
            }
            else
            {
                $records = $this->getAdapter()->select()
                        ->from($fromTableArr)
                        ->joinLeft($otherTableArr, $joinCond)
                        ->where($whereCond);
            }

            if ($limit != NULL)
                $records = $records->limit($limit);

            if ($order != NULL)
                $records = $records->order($order);

//            prd($records->__toString());

            $records = $records->query()->fetchAll();
            return $records;
        }

        public function getAllRecordFromInnerJoin($fromTableArr, $otherTableArr, $joinCond, $whereCond = NULL, $order = NULL, $limit = NULL, $paginator = NULL)
        {

            if ($whereCond == NULL)
            {
                $records = $this->getAdapter()->select()
                        ->from($fromTableArr)
                        ->joinInner($otherTableArr, $joinCond);
            }
            else
            {
                $records = $this->getAdapter()->select()
                        ->from($fromTableArr)
                        ->joinInner($otherTableArr, $joinCond)
                        ->where($whereCond);
            }

            if ($limit != NULL)
                $records = $records->limit($limit);

            if ($order != NULL)
                $records = $records->order($order);

            //prd($records->__toString());
            if ($paginator != NULL)
                return $records;

            $records = $records->query()->fetchAll();
            return $records;
        }

        public function insertRecordData($data_array, $tableName = NULL)
        {
            if ($tableName != NULL)
                $this->_name = $tableName;

            return $this->insert($data_array);
        }

        public function updateRecordData($data_array, $whereCond, $tableName = NULL)
        {
            if ($tableName != NULL)
                $this->_name = $tableName;

//prd($whereCond);
            return $this->update($data_array, $whereCond);
        }

        public function deleteRecord($whereCond, $tableName = NULL)
        {
            if ($tableName != NULL)
                $this->_name = $tableName;

            $this->delete($whereCond);
        }

        public function getRecordMaxId($idFieldName, $tableName)
        {
            $this->_name = $tableName;

            $maxid = $this->select()
                            ->from($this, array(new Zend_Db_Expr("max($idFieldName) as maxId")))
                            ->query()->fetch();
            return $maxid;
        }

        public function getdata($tableName, $whereCond = 1)
        {
            $this->_name = $tableName;

            return $this->select()->from($tableName)->where($whereCond)->query()->fetch();
        }

        public function is_exists($tableName, $whereCond = 1, $debug = FALSE)
        {
            $this->_name = $tableName;

            $record = $this->select()->where($whereCond);

            if ($debug == TRUE)
                prd($record->__toString());

            $record = $record->query()->fetch();
            return $record;
        }

        public function updateAndReplaceData($tableName, $columnName, $toReplaceName, $fromReplaceName, $whereString)
        {
            $sql = "UPDATE $tableName  SET $columnName = REPLACE($columnName, ',$toReplaceName', '$fromReplaceName') WHERE $whereString";
            $this->getAdapter()->query($sql);
        }

        public function getLastInsertId($tableName)
        {
            return $this->getAdapter()->lastInsertId($tableName);
        }

    }